package com.zhh.dao;

import com.zhh.pojo.TbUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserDao extends JpaRepository<TbUser,Long> {


    /**
     *
     * @param username
     * @return
     */
    @Query("from TbUser u where u.username =:username")
    TbUser findbyUsername(@Param("username") String username);


    /**
     * 查询用户的权限列表
     * @param username
     * @return
     */
    @Query(value = "select p.permission from tb_user u join  tb_role r on u.role_id = r.id " +
            "JOIN tb_role_perm rp ON rp.role_id = u.role_id " +
            "JOIN tb_permission p ON rp.permission_id = p.id " +
            "where u.username = :username",nativeQuery = true)
    List<String> getPermissions(@Param("username") String username);

    /**
     * 查询user的角色名
     * @param username
     * @return
     */
    @Query(value = "select r.name from tb_user u join  tb_role r on u.role_id = r.id where u.username = :username",nativeQuery = true)
    List<String> getRoles(@Param("username") String username);
}
